﻿using AutoMapper;
using B.S.BsicData.Api.Writer.Application.Command;
using B.S.BsicData.Domain;
using B.S.BsicData.ErrorCode.Entity;
using B.S.BsicData.Infartrans;
using MediatR;

namespace B.S.BsicData.Api.Writer.Application.CommandHandler
{
    public class CreateDepentCommandHandler : IRequestHandler<CreateDepentCommand, ApiResult<int>>
    {
        private readonly IBaseReository<DementperModel> deRepo;
        private readonly IMapper mapper;

        public CreateDepentCommandHandler(IBaseReository<DementperModel> deRepo, IMapper mapper)
        {
            this.deRepo = deRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<int>> Handle(CreateDepentCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            var rest = mapper.Map<DementperModel>(request);
            var user = deRepo.AddAsync(rest).Result;
            if (user > 0)
            {
                result.Code = ApiENums.成功;
                result.Message = "成功";
                result.Data = 1;
            }
            else
            {
                result.Code = ApiENums.失败;
                result.Message = "失败";
                result.Data = -1;
            }
            return Task.FromResult(result);
        }
    }
}
